// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    subjectId: '',
  },

  signup:function() {
    var that = this
    wx.showLoading({
      title: '',
    })
    wx.request({
      url: 'https://vcloud2.vivalink.com/auth',
      method: "POST",
      data:{
        'id':"6170706009be6b1f2045cbac77",
        'key':"Whn_Nla;UtMLt@uUsQL]PDLx^?h46n<ri?v`K[@D"
      },
      header:{
        "content-Type": "application/json",
      },
      timeout: 20000,
      dataType:'json',
      success (res) {
        console.log(res.data)
        var response = res.data
        console.log("code:",response['code'])
        if (response['code'] == 200) {
          var token = response['data']['token'];
          console.log(token)
          that.register(token)
        }
      },
      fail(res){

      },
    })
  },
  register(token){
    console.log('开始注册请求:',token)
    let urlString = 'https://vcloud2.vivalink.com/tenants/VivaLNK/subjects/' + this.data.subjectId
    console.log(urlString)
    wx.request({
      url: urlString,
      method:"POST",
      data:{

      },
      header:{
        "Authorization":token,
      },
      timeout:60000,
      dataType:'json',
      success(res){
        wx.hideLoading({
          success: (res) => {},
        })
        console.log('register:',res.data)
        if (res.data['code'] == 200) {
          wx.switchTab({
          url: '../dashboard/dashboard',
          })
          wx.setStorageSync('login', true)
        }
      },
      fail(res){
        console.log("失败:",res)
      }
    })
  },

  getSubjectId:function (e) {
    this.setData({
      subjectId:e.detail.value,
    })
    console.log(e.detail.value)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    try{
      var login = wx.getStorageSync("login")
      console.log(login)
      if (login) {
        wx.switchTab({
          url: '../dashboard/dashboard',
        })
      }
    }catch(e){

    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})